package cn.leftsite.juc.tools;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Condition 示例
 *
 * @author wangjinqiang
 * @date 2019-06-03
 */
public class ReentrantLockCondition implements Runnable {
    private static final ReentrantLock lock = new ReentrantLock();
    private static final Condition condition = lock.newCondition();

    @Override
    public void run() {
        try {
            lock.lock();
            condition.await();
            System.out.println("Thread is going on");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockCondition r1 = new ReentrantLockCondition();
        Thread t1 = new Thread(r1);
        t1.start();
        // 通知线程继续执行
        Thread.sleep(2000);
        lock.lock();
        condition.signal();
        lock.unlock();
    }
}
